import {
  querySensitiveWindowList,
  addSensitiveWindow,
  updateSensitiveWindow,
  deleteSensitiveWindow,
} from '@/services/SensitiveWindow';

export default {
  namespace: 'SensitiveWindow',

  state: {
    windowList: {},
    updateDidFinish: false,
  },

  effects: {
    *querySensitiveWindowList({ payload }, { call, put }) {
      const response = yield call(
        querySensitiveWindowList,
        payload.page_num,
        payload.page_size,
        payload.type
      );
      yield put({
        type: 'setPayload',
        payload: {
          windowList: response.data,
        },
      });
    },
    *addSensitiveWindow({ payload }, { call, put }) {
      const response = yield call(
        addSensitiveWindow,
        payload.report_type,
        payload.published_state,
        payload.published_time
      );
      yield put({
        type: 'setPayload',
        payload: {
          updateDidFinish: new Date().getTime(),
        },
      });
    },
    *updateSensitiveWindow({ payload }, { call, put }) {
      const response = yield call(
        updateSensitiveWindow,
        payload.report_type,
        payload.published_state,
        payload.published_time,
        payload.id
      );
      yield put({
        type: 'setPayload',
        payload: {
          updateDidFinish: new Date().getTime(),
        },
      });
    },
    *deleteSensitiveWindow({ payload }, { call, put }) {
      const response = yield call(deleteSensitiveWindow, payload.id);
      yield put({
        type: 'setPayload',
        payload: {
          updateDidFinish: new Date().getTime(),
        },
      });
    },
  },

  reducers: {
    setPayload(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
